package com.lge.pitchingcoins.util;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager.WifiLock;
import android.provider.Settings;
import android.util.DisplayMetrics;

import com.lge.pitchingcoins.util.lockkey.HomeKeyLockForKK;
import com.lge.pitchingcoins.util.lockkey.HomeKeyLockForLessThanKK;
import com.lge.pitchingcoins.util.lockkey.IHomeKeyLock;
import com.lge.pitchingcoins.util.lockkey.IWindowManagerLayoutParams;
import com.lge.pitchingcoins.util.lockkey.WindowManagerForKK;
import com.lge.pitchingcoins.util.lockkey.WindowManagerForLessThanKK;

public class Util {
    public static final int PC_PORT = 10004;
    public static final String CLASS_NAME_KEYEXCEPTIONCONSTANTS = "com.lge.constants.KeyExceptionConstants";
    public static boolean showFPS = true;
    
    public static double getDeviceScreenSize(DisplayMetrics dm) {
        double x = Math.pow(dm.widthPixels / dm.xdpi, 2);
        double y = Math.pow(dm.heightPixels / dm.ydpi, 2);

        return Math.sqrt(x + y);
    }

    public static boolean isDeviceScreenIsLargerThanArgument(DisplayMetrics dm, double targetSize) {
        double deviceInch = getDeviceScreenSize(dm);
        double minThreshold = targetSize - 0.1;

        return (minThreshold < deviceInch);
    }

    public static void lockWithWIFI(Context context) {
        WifiLock mWifiLock = null;
        WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);

        mWifiLock = wifiManager.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF, "MyLock");

        if (!mWifiLock.isHeld()) {
            mWifiLock.acquire();
        }
    }
    
    public static void unlockWithWIFI(Context context) {
        WifiLock mWifiLock = null;
        WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);

        mWifiLock = wifiManager.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF, "MyLock");

        if (mWifiLock.isHeld()) {
            mWifiLock.release();
        }
    }

    public static boolean isClass(String className) {
        try {
            if (Class.forName(className) != null) {
                return true;
            }
        } catch (ClassNotFoundException e) {
            return false;
        }
        
        return false;
    }
    
    public static void setHomeKeyLock(Activity activity, boolean isLocked) {
        IWindowManagerLayoutParams windowManagerLayoutParams = null;
        IHomeKeyLock homeKeyLock = null;
        
        boolean isOSVersionKK = isClass(CLASS_NAME_KEYEXCEPTIONCONSTANTS);
        
        if (isOSVersionKK) {
            windowManagerLayoutParams = new WindowManagerForKK(activity.getWindow().getAttributes());
            homeKeyLock = new HomeKeyLockForKK();
        } else {
            windowManagerLayoutParams = new WindowManagerForLessThanKK(activity.getWindow().getAttributes());
            homeKeyLock = new HomeKeyLockForLessThanKK();
        }
        
        homeKeyLock.setHomeKeyLock(isLocked, windowManagerLayoutParams, activity);
    }
    
    public static String getDeviceName(ContentResolver contentResolver) {
        // First parameter should be getContentResolver() in Activity.
        return Settings.System.getString(contentResolver, "lg_device_name");
    }
}